<?php

namespace app\model;

use think\Db;
use think\Model;
use think\Validate;

class Attachment extends Model
{
    protected $table = 'yx_sys_attachment';
    protected $pk = 'id';

    public function add($data)
    {
        if ($data) {
            $add_data['filename'] = $data['filename'];
            $add_data['attachment'] = $data['attachment'];
            $add_data['type'] = $data['type'];
            $add_data['mpid'] = $data['mpid'];
            if (isset($data['module']) && $data['module']) {
                $add_data['module'] = $data['module'];
            }
            $add_data['group_id'] = $data['group_id'];
            $add_data['createtime'] = $data['createtime'];
            $res = Db::table('yx_sys_attachment')->insert($add_data);
            return $res;
        }
    }

    //根据module 查询列表
    public function getList()
    {
        global $_P;
        $pageSize = 50;
        $param = input('param.');
        $pageNumber = max(1, intval($param['page']));
        $module = intval(substr($_P['m'], 2)) * 1000 + intval($_P['themeid']);
        $module2 = intval(substr($_P['m'], 2)) * 1000;
        $type = isset($param['type']) && $param['type'] ? $param['type'] : 1;
        $list = $this->field(['id', 'filename as title', 'attachment as url', 'createtime'])
            ->where([
                'type' => $type
            ])
            ->where("`module` = " . $module . " OR `module` = " . $module2)
            ->order(['createtime' => 'desc', 'filename' => 'asc'])
            ->paginate($pageSize, false, ['page' => $pageNumber]);
        $total_page = $list->lastPage();
        $list = $list->toArray();
        $list['total_page'] = $total_page;
        return $list;
    }

}